package com.xxx.modules.controller;;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxx.modules.entity.Adjust;
import com.xxx.modules.mapper.AdjustMapper;
import com.xxx.modules.mapper.ApplyMapper;
import com.xxx.modules.service.ApplyService;
import com.xxx.modules.entity.Apply;
import com.xxx.modules.utils.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import com.github.pagehelper.PageInfo;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import com.xxx.modules.utils.Result;
import com.xxx.modules.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * apply
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-04-29
 */
@RestController
@RequestMapping("/common/apply")
@Api(tags="apply")
public class ApplyController {
    @Autowired
    private ApplyService applyService;

    /**
    *  获取所有申请信息
    * @param pageNum
    * @param pageSize
    * @param apply
    * @return
    */

    @GetMapping("/getApplyInfo")
    @ApiOperation("获取所有申请信息")
    public Result getApplyInfo(Apply apply, @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value="pageSize",defaultValue = "10") Integer pageSize){
        PageInfo<Apply> data = applyService.selectApplyInfo(apply,pageNum,pageSize);
        return ResultUtil.success(1,"成功",data);
    }
    @Autowired
    private ApplyMapper applyMapper;
    @Autowired
    private AdjustMapper adjustMapper;
    /**
    * 保存申请信息
    * @param apply
    * @return
    */
    @PostMapping("/saveApplyInfo")
    @ApiOperation("保存申请信息")
    public Result saveApplyInfo(@RequestBody Apply apply){

        Adjust adjust = adjustMapper.selectById(apply.getAdjustId());
        //根据用户选择的调剂信息去查找该调剂是否还有名额
        if (adjust.getCount() < 1){
            return ResultUtil.error(-1,"调剂人数已满");
        }
        QueryWrapper<Apply> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",apply.getUserId()).eq("adjust_id",apply.getAdjustId());
        //用户是否已经申请了该调剂院校了
        Apply apply1 = applyMapper.selectOne(wrapper);
        if (apply1 !=null){
            return ResultUtil.error(-1,"您已经申请过该院校信息了");
        }
        apply.setCreateTime(TimeUtil.getCurrentTime());
        apply.setStatus(1);
        applyService.saveApplyInfo(apply);
        return ResultUtil.success(1,"成功",null);
    }


    /**
     * 更新申请信息
     * @param apply
     * @return
     */

    @PutMapping("/updateApplyInfo")
    @Transactional
    @ApiOperation("更新申请信息")
    public Result updateApplyInfo(@RequestBody Apply apply){
        applyService.updateApplyInfo(apply);
        if (apply.getStatus() == 2){
            //申请通过
            Adjust adjust = adjustMapper.selectById(apply.getAdjustId());
            if (adjust.getCount() < 1){
                return ResultUtil.error(-1,"调剂人数已满");
            }
            adjust.setCount(adjust.getCount() -1);
            adjustMapper.updateById(adjust);
        }
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id删除申请信息
     * @param id
     * @return
     */
    @DeleteMapping("/delApplyInfo")
    @ApiOperation("根据id删除申请信息")
    public Result delApplyInfo(Integer id){
        applyService.delApplyInfo(id);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id集合批量删除申请信息
     * @param idList
     * @return
     */
    @DeleteMapping("/delBatchApplyInfo")
    @ApiOperation("根据id集合批量删除申请信息")
    public Result delBatchApplyInfo(String idList){
        applyService.delBatchApplyInfo(idList);
        return ResultUtil.success(1,"成功",null);
    }

}